package com.whereistheparty;

import java.util.Calendar;
import java.util.GregorianCalendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.Toast;

public class AddPartyActivity2 extends Activity 
{
	private DatePicker datepicker;	
	private Spinner spnrPartyStartTime;
	private Spinner spnrPartyFinishTime;
	private Boolean rigthDate = false;
	private Boolean sonrakigun = false;
	
	
	 public void onCreate(Bundle savedInstanceState) 
	 {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.addparty2);
	        
	        ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,WhereisthepartyActivity.PartyHours);    
	        ArrayAdapter bb = new ArrayAdapter(this,android.R.layout.simple_spinner_item,WhereisthepartyActivity.PartyHours);  
	           
	        
	        spnrPartyStartTime= (Spinner)findViewById(R.id.spnrPartyStartTime);   
	        spnrPartyFinishTime= (Spinner)findViewById(R.id.spnrPartyFinishTime);
	        datepicker= (DatePicker)findViewById(R.id.datePicker1); 
	        
			
	        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);      
	        spnrPartyStartTime.setAdapter(aa);                 
	        bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);      
	        spnrPartyFinishTime.setAdapter(bb);   
	        	        
	        manageButtons();	        
	 }
		private void addparty(Boolean gun)
		{
			WhereisthepartyActivity party = new WhereisthepartyActivity();
			
			WhereisthepartyActivity.PartyFinishTime =spnrPartyFinishTime.getSelectedItem().toString();				
			WhereisthepartyActivity.PartyStartingTime =spnrPartyStartTime.getSelectedItem().toString();
			if(gun==true)
			{
				WhereisthepartyActivity.partydate=Integer.toString(datepicker.getYear())+"-"+Integer.toString(datepicker.getMonth()+1)+"-"+Integer.toString((datepicker.getDayOfMonth()));				
				WhereisthepartyActivity.partyfinishdate=Integer.toString(datepicker.getYear())+"-"+Integer.toString(datepicker.getMonth()+1)+"-"+Integer.toString((datepicker.getDayOfMonth()+1));
			}
			else
			{
				WhereisthepartyActivity.partydate=Integer.toString(datepicker.getYear())+"-"+Integer.toString(datepicker.getMonth()+1)+"-"+Integer.toString((datepicker.getDayOfMonth()));			
				WhereisthepartyActivity.partyfinishdate=Integer.toString(datepicker.getYear())+"-"+Integer.toString(datepicker.getMonth()+1)+"-"+Integer.toString(datepicker.getDayOfMonth());	
			}
			party.addPartyFunc();
			Intent i = new Intent(getApplicationContext(),
			WhereisthepartyActivity.class);
			startActivity(i);						
			finish();
			
		}
	private void manageButtons()
	{
		 Button btnCancel = new Button(AddPartyActivity2.this);
		 Button btnForward = new Button(AddPartyActivity2.this);
		 
		 btnCancel = (Button)findViewById(R.id.btnBack); 
		 btnForward = (Button)findViewById(R.id.btnAddParty); 
		 
		 btnCancel.setOnClickListener(new OnClickListener()
		 {			
			public void onClick(View v) 
			{
				Intent i = new Intent(getApplicationContext(),
						AddPartyActivity.class);
				
				startActivity(i);
				finish();
			}
		});
		 btnForward.setOnClickListener(new OnClickListener()
		 {				
			 public void onClick(View v) 
				{
				    WhereisthepartyActivity party = new WhereisthepartyActivity();
					WhereisthepartyActivity.PartyStartingTime =spnrPartyStartTime.getSelectedItem().toString();
					String expo=WhereisthepartyActivity.PartyStartingTime;
					String delims = "[:]";
					String[] saatdurumu = expo.split(delims);
					int saat=Integer.parseInt(saatdurumu[0]);
					int dakika=Integer.parseInt(saatdurumu[1]);
					WhereisthepartyActivity.PartyFinishTime =spnrPartyFinishTime.getSelectedItem().toString();
					String expoa=WhereisthepartyActivity.PartyFinishTime;
					String delimsa = "[:]";
					String[] saatdurumua = expoa.split(delimsa);
					int saata=Integer.parseInt(saatdurumua[0]);
					int dakikaa=Integer.parseInt(saatdurumua[1]);
					Calendar cal = Calendar.getInstance();
					int day = cal.get(Calendar.DATE);
					int month = cal.get(Calendar.MONTH) + 1;
					int year = cal.get(Calendar.YEAR);
					
					if(saata<saat)
					{
						sonrakigun=true;
						
					}
					else if(saata==saat)
					{
						if(dakika<dakikaa)
						{
							sonrakigun=true;
						}
					}
					if( datepicker.getYear()>year)
					{
						rigthDate = true;
						addparty(sonrakigun);
					}
					else if(datepicker.getYear()==year)
					{
						if(datepicker.getMonth()+1 > month)
						{
							rigthDate = true;
							addparty(sonrakigun);
						}
						else if(datepicker.getMonth()+1 == month)
						{
							if(datepicker.getDayOfMonth()> day)
							{			
								rigthDate = true;
								addparty(sonrakigun);
							}	
							else if(datepicker.getDayOfMonth()== day)
							{
								
								rigthDate = true;
								Calendar cala = new GregorianCalendar();
								int hour24 = cala.get(Calendar.HOUR_OF_DAY);    
								int min = cala.get(Calendar.MINUTE); 
								
								
								
								if(saat>hour24)
								{
									party.addPartyFunc();
									Intent i = new Intent(getApplicationContext(),
									WhereisthepartyActivity.class);
									startActivity(i);						
									finish();
								}
								else if(saat==hour24)
								{
									if(dakika>min)
									{
										rigthDate = true;
										addparty(sonrakigun);
									}
									else
									{
										rigthDate = false;
									}
								}
							}
						}
							else
							{
								rigthDate = false;
							}
						}			
					
					if(rigthDate== false)
					{
						Toast.makeText(AddPartyActivity2.this, "Please enter valid date", Toast.LENGTH_SHORT).show();
					}

				}
			 

			
		});
			
	}
				
		
	

}
